# TODO: Add comment
# 
# Author: rogb
###############################################################################


setClass("ZeroRateSpread",
		contains=c("SituationDate","BasicInstrument","BasicDateData")
)

ZeroRateSpread <- function(SituationDate,Currency,Name,Date,Data){
	nCurrency <- length(Currency)
	if(missing(Name)){
		Name=character(nCurrency)
	}
	nName <- length(Name)
	if(nCurrency!=nName & (nName>0 &nCurrency>0)){
		stop("Name and Currency have different length")
	}
	nDate <- length(Date)
	Data <- as.matrix(Data)
	
	Dim <- dim(Data)
	nRowData <- Dim[1]
	nColData <- Dim[2]
	if((nRowData!=nDate) | (nColData!=nCurrency)){
		stop("Factor, Name and Currency have different dimension")
	}
	if(SituationDate>min(Date)){
		stop("Date and Situation Date are incompatible")
	}
	new("ZeroRateSpread",SituationDate=SituationDate,Currency=Currency,Name=Name,Date=Date,Data=Data)
}

# ZS <- ZeroRateSpread(Sys.Date(),c("CHF","EUR"),c("AAA","BBB"),Sys.Date()+(0:9)*500+1,cbind(-runif(10)*5,rep(5,10)))
# tmp <- as.DiscountFactor(ZS) 
# ZS2 <- as.ZeroRateSpread(tmp) 
# ZS <- ZeroRateSpread(Sys.Date(),c("CHF"),c("AAA"),plusYear(Sys.Date(),c(1,3)),c(-1,2))
# InputDiscountFactor <- as.DiscountFactor(ZS)

setMethod("show","ZeroRateSpread",function(object){
			tmp <- round(object@Data,4)
			rownames(tmp) <- as.character(object@Date)
			colnames(tmp) <- info(object)
			cat("Zero Rate Spread as of ",as.character(object@SituationDate),"\n",sep="")
			print(tmp)
		})

#forwardZeroRateSpread <- function(ForwardSituationDate,InputZeroRateSpread){
#	as(forwardDiscountFactor(ForwardSituationDate,as(InputZeroRateSpread,"DiscountFactor")),"ZeroRateSpread")
#}


setMethod("plot",signature=c("ZeroRateSpread"),function(x,y,...){
			n <- dim(x)[2]
			plotTitle <- paste("Zero Rate Spread as of",as.character(x@SituationDate))
			#ylim <- range(x@Rate)
			ylim <- c(min(x@Data),max(x@Data))
			plot(x@Date,x@Data[,1],main=plotTitle,typ="l",ylab="Zero Rate",xlab="Date",lwd=2,ylim=ylim,...)
			for(i in 1:n){
				lines(x@Date,x@Data[,i],col=i,lwd=2,...)
			}
			legend("bottomright",paste(x@Currency,x@Name),col=1:n,lwd=2,...)
		})



setMethod("[",signature=c("ZeroRateSpread","Date","numeric"),function(x,i,j,drop){
			ZRC <- ZeroRateCurve(x@SituationDate,x@Currency[j],x@Name[j],x@Date,x@Data[,j])
			DF <- as(ZRC,"DiscountFactor")
			if(length(i)==1 && i==x@SituationDate){
				Result <- as(getDiscountFactor(i+0:1,DF),"ZeroRateSpread")
				Result <- Result[1,]
			}
			else{
				Result <- as(getDiscountFactor(i,DF),"ZeroRateSpread")
			}
			Result
		})

setMethod("[",signature=c("ZeroRateSpread","Date","missing"),function(x,i,j,drop){
			DF <- as(x,"DiscountFactor")
			if(length(i)==1 && i==x@SituationDate){
				Result <- as(getDiscountFactor(i+0:1,DF),"ZeroRateSpread")
				Result <- Result[1,]
			}
			else{
				Result <- as(getDiscountFactor(i,DF),"ZeroRateSpread")
			}
			Result
		})

setMethod("[",signature=c("ZeroRateSpread","Date","character"),function(x,i,j,drop){
			idx <- is.element(x@Currency,j)
			ZRC <- ZeroRateCurve(x@SituationDate,x@Currency[idx],x@Name[idx],x@Date,x@Data[,idx])
			DF <- as(ZRC,"DiscountFactor")
			as(getDiscountFactor(i,DF),"ZeroRateSpread")
		})

# df2 <- DiscountFactor(Sys.Date(),c("CHF","EUR"),c("AAA","BBB"),Sys.Date()+(0:9)*500,cbind(1*0.9^(0:9),1*0.85^(0:9)))
# zr1 <- as.ZeroRateCurve(df2)
# zr2 <- ZeroRateCurve(Sys.Date(),c("CHF","EUR"),c("AAA","BBB"),Sys.Date()+(0:9)*500,cbind(runif(10)*5,rep(5,10)))

